package com.splant.android.smart.UILevel.Community.fragments;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.splant.android.R;
import com.splant.android.smart.ModelLevel.WaterListBean;
import com.splant.android.smart.SystemDefinedLevel.LRecyclerview.base.BaseMultiAdapter;
import com.splant.android.smart.SystemDefinedLevel.LRecyclerview.base.SuperViewHolder;
import com.splant.android.smart.UILevel.Utils.sPlantUitl;

public class Garden_WaterList_Adapter extends BaseMultiAdapter<WaterListBean.Water> {

    public Garden_WaterList_Adapter(Context context) {
        super(context);
        addItemType(0,R.layout.garden_waterlist_item);
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, final int position) {

        RelativeLayout rl_water=holder.getView(R.id.rl_water);
        ImageView img_water=holder.getView(R.id.img_water);
        TextView tv_waterName=holder.getView(R.id.tv_waterName);
        TextView tv_switch0=holder.getView(R.id.tv_switch0);
        TextView tv_switch1=holder.getView(R.id.tv_switch1);
        TextView tv_switch2=holder.getView(R.id.tv_switch2);
        ImageView img_electricity=holder.getView(R.id.img_electricity);
        TextView tv_waterState=holder.getView(R.id.tv_waterState);
        TextView tv_openAreaFailure=holder.getView(R.id.tv_openAreaFailure);

        WaterListBean.Water water=getDataList().get(position);

        int proType= sPlantUitl.getWaterType(water.getWaterId());

        tv_waterName.setText(water.getWaterName());

        if (water.getWaterState()==1)
        {
            if (proType==1)
            {
                tv_switch0.setVisibility(View.VISIBLE);
                tv_switch1.setVisibility(View.INVISIBLE);
                tv_switch2.setVisibility(View.INVISIBLE);
                img_water.setImageResource(R.mipmap.garden_ic_water1_online);
            }
            else if (proType==2)
            {
                tv_switch0.setVisibility(View.VISIBLE);
                tv_switch1.setVisibility(View.VISIBLE);
                tv_switch2.setVisibility(View.INVISIBLE);
                img_water.setImageResource(R.mipmap.garden_ic_water2_online);
            }
            else
            {
                tv_switch0.setVisibility(View.VISIBLE);
                tv_switch1.setVisibility(View.VISIBLE);
                tv_switch2.setVisibility(View.VISIBLE);
                img_water.setImageResource(R.mipmap.garden_ic_water3_online);
            }

            int[] switchsTime=water.getSwitchsTime();
            if (switchsTime!=null)
            {
                if (switchsTime.length>=1){

                    if (switchsTime[0]>0)
                        tv_switch0.setBackgroundResource(R.color.garden_switch_on);
                    else
                        tv_switch0.setBackgroundResource(R.color.garden_switch_off);

                }
                if (switchsTime.length>=2){

                    if (switchsTime[1]>0)
                        tv_switch1.setBackgroundResource(R.color.garden_switch_on);
                    else
                        tv_switch1.setBackgroundResource(R.color.garden_switch_off);
                }
                if (switchsTime.length>=3){
                    if (switchsTime[2]>0)
                        tv_switch2.setBackgroundResource(R.color.garden_switch_on);
                    else
                        tv_switch2.setBackgroundResource(R.color.garden_switch_off);
                }
            }

            img_electricity.setVisibility(View.VISIBLE);
            tv_waterState.setVisibility(View.INVISIBLE);
        }
        else
        {

            if (proType==1)
            {
                tv_switch0.setVisibility(View.VISIBLE);
                tv_switch1.setVisibility(View.INVISIBLE);
                tv_switch2.setVisibility(View.INVISIBLE);

                img_water.setImageResource(R.mipmap.garden_ic_water1_offline);
                tv_switch0.setBackgroundResource(R.color.garden_switch_off);
            }
            else if (proType==2)
            {
                tv_switch0.setVisibility(View.VISIBLE);
                tv_switch1.setVisibility(View.VISIBLE);
                tv_switch2.setVisibility(View.INVISIBLE);

                img_water.setImageResource(R.mipmap.garden_ic_water2_offline);
                tv_switch0.setBackgroundResource(R.color.garden_switch_off);
                tv_switch1.setBackgroundResource(R.color.garden_switch_off);
            }
            else
            {
                tv_switch0.setVisibility(View.VISIBLE);
                tv_switch1.setVisibility(View.VISIBLE);
                tv_switch2.setVisibility(View.VISIBLE);

                tv_switch0.setBackgroundResource(R.color.garden_switch_off);
                tv_switch1.setBackgroundResource(R.color.garden_switch_off);
                tv_switch2.setBackgroundResource(R.color.garden_switch_off);

                img_water.setImageResource(R.mipmap.garden_ic_water3_offline);
            }

            img_electricity.setVisibility(View.INVISIBLE);
            tv_waterState.setVisibility(View.VISIBLE);

            if (water.getWaterState()==3)
                tv_waterState.setText("未组网");
            else if (water.getWaterState()==2)
                tv_waterState.setText("离线");


        }

        if (water.getOpenCloseFailure()==1)
        {
            tv_openAreaFailure.setVisibility(View.VISIBLE);
            tv_openAreaFailure.setText("打开失败");
        }
        else if (water.getOpenCloseFailure()==2)
        {
            tv_openAreaFailure.setVisibility(View.VISIBLE);
            tv_openAreaFailure.setText("关闭失败");
        }
        else
        {
            tv_openAreaFailure.setVisibility(View.GONE);
        }

        if (water.getElectricity()<=25)
        {
            img_electricity.setImageResource(R.mipmap.garden_ic_electricity_1);
        }
        else if (water.getElectricity()<=50)
        {
            img_electricity.setImageResource(R.mipmap.garden_ic_electricity_2);
        }
        else if (water.getElectricity()<=75)
        {
            img_electricity.setImageResource(R.mipmap.garden_ic_electricity_3);
        }
        else
        {
            img_electricity.setImageResource(R.mipmap.garden_ic_electricity_4);
        }

        rl_water.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             if (mListener!=null)
                 mListener.onItemChick(position);
            }
        });

    }

    private onItemClickListener mListener = null;

    public void setOnItemClickListener(onItemClickListener mListener)
    {
        this.mListener=mListener;
    }

    public interface onItemClickListener {
        void onItemChick(int pos);
    }

}
